package com.letou.ls.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.table.PreferencesUtils;
import org.apache.table.TableTitle;

import com.letou.ls.R;
import com.letou.ls.mode.AppConfigInfo;
import com.letou.ls.mode.LotteryInfo;
import com.letou.ls.uitl.AppConfigManager;
import com.letou.ls.uitl.Constant;
import com.letou.ls.view.TableBadyView;
import com.letou.ls.view.TableBottmBady;
import com.letou.ls.view.TableBottomTile;
import com.letou.ls.view.TableTitleView;
import com.letou.ls.view.TableViewUtil;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 十一选五 竖直
 *
 * @author Administrator
 *
 */
public class FifFiveActivity extends BaseActivity {
    LinearLayout mTopLayout;
    LinearLayout mBottomLayout;
    LinearLayout mContextLayout;
    private TableBadyView mTableBadyView;
    float textSize;
    private float itemHeight;
    private ArrayList<Float> widthList;
    private TableBottmBady mTableBottmBady;
    private float density;
    
    private ScrollView scrollview;
    private TextView bottomText;
    
    private TextView startLotto;
    private ImageButton menuBtn;
    private LinearLayout menuParent;
    private  AppConfigInfo mInfo;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 添加 标记
        lotteryCode = Constant.LOTTERY_CODE_04;
        screen_direction_type = 0;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fif_five);
        initFirstDateTime("9:04:59", "9:15:00");
        loadVerticalLayout();
        
        // if (dataVar == null) {
        // dataVar = loadDataVar();
        // }
        //
        // if (bottomdataVar == null) {
        // bottomdataVar = getBottomdataVar();
        // }
        // mTableBadyView.updateData(dataVar, widthList, textSize * density,
        // itemHeight);
        //
        // // ArrayList<Float> list =
        // // TableViewUtil.getBottomWidthForVer(widthList);
        // ArrayList<Float> list = TableViewUtil.getBottomWidth(widthList, 4,
        // 26, 29);
        // mTableBottmBady.addData(bottomdataVar, list, textSize,
        // R.drawable.icon_115);
        
    }
    
    private void loadVerticalLayout() {
        mTopLayout = (LinearLayout) findViewById(R.id.fif_five_top_layout);
        mContextLayout = (LinearLayout) findViewById(R.id.fif_five_context_layout);
        mBottomLayout = (LinearLayout) findViewById(R.id.fif_five_bottom_layout);
        scrollview = (ScrollView) findViewById(R.id.fif_five_scrollview);
        
        bottomText = (TextView) findViewById(R.id.fif_five_bottom_text);
        menuParent = (LinearLayout) findViewById(R.id.menuParent);
        menuBtn = (ImageButton) findViewById(R.id.bottom_menu_bt);
        
        startLotto = (TextView) findViewById(R.id.startLotto);
        
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        
        // int height = metric.heightPixels; // 屏幕高度（像素）
        density = metric.density; // 屏幕密度（0.75 / 1.0 / 1.5）
        // int densityDpi = metric.densityDpi; // 屏幕密度DPI（120 / 160 / 240）
        int viewWidth = metric.widthPixels;
        
        TableTitle title = TableViewUtil.get115Table(this, viewWidth);
        
        // widthList = title.getmWidthList();
        
        initWidthList(viewWidth);
        TableTitleView tableTopTitle = new TableTitleView(this, widthList);
        LinearLayout.LayoutParams ll =
                new LinearLayout.LayoutParams(viewWidth, LayoutParams.WRAP_CONTENT);
        tableTopTitle.addData(title, density,mInfo);
        tableTopTitle.setLayoutParams(ll);
        
        textSize = title.getDataTextSize();
        
        mTableBadyView = new TableBadyView(this, widthList);
        
        // itemHeight = textSize * 1.618f * density;
        itemHeight = widthList.get(5);
        int size = 50;
        int tableHeight = (int) (itemHeight * size + size + 1);
        
        LinearLayout.LayoutParams badyParams =
                new LinearLayout.LayoutParams(viewWidth, tableHeight);
        mTableBadyView.setLayoutParams(badyParams);
        
        TableBottomTile tableBottom = new TableBottomTile(this);
        tableBottom.setmWidthList(widthList);
        tableBottom.addData(title, density,mInfo);
        tableBottom.setLayoutParams(ll);
        
        mTopLayout.addView(tableTopTitle);
        mContextLayout.addView(mTableBadyView, badyParams);
        mBottomLayout.addView(tableBottom);
        
        mTableBottmBady = new TableBottmBady(this);
        mTableBottmBady.setLayoutParams(ll);
        
        mBottomLayout.addView(mTableBottmBady);
        
        scrollview.setOnTouchListener(new OnTouchListener() {
            
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                
                // Logger.v("TEST", " scrollview " + arg1.getAction());
                return mGesture.onTouchEvent(arg1);
                // TODO Auto-generated method stub
            }
            
        });
        
        String dateline = "";
        // if (Constant.getCurrentLottery() != null) {
        // dateline = " 截止日期:" + Constant.getExpirationTime();
        // dateline = dateline + " 当前版本号:" + Constant.getCurrentVersion(this);
        // dateline = dateline + " 站号:" + PreferencesUtils.getString(this,
        // Constant.PRE_TAG_USERNAME);
        // }
        // String text = "开奖信息以彩票中心为准，此信息仅供参考！ " + dateline;
        if (Constant.getCurrentLottery() != null) {
            dateline = "       截止日期:" + Constant.getExpirationTime();
            dateline = dateline + "       站号:"
                    + PreferencesUtils.getString(this, Constant.PRE_TAG_USERNAME);
            dateline = dateline + "       版本号:" + Constant.getCurrentVersion(this);
        }
        String text = "     开奖信息以彩票中心为准，此信息仅供参考！ " + dateline;
        bottomText.setText(text);
        menuBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                gotoMenuActivity();
            }
            
        });
    }

    private void initWidthList(int viewWidth) {
    	mInfo = AppConfigManager.getInstance(FifFiveActivity.this).getAppConfigInfo();
        // 必选 期号
        // 互斥 前三直 和 开奖号码互斥 2位 和 3位 显示折线 和 开奖号码分布
        // 至少选1个 开奖号码分布 和 前三分布
        // 可选 和值 跨度 大号个数 单号个数 推荐号 遗漏值 出现次数
        Log.e("cc", "initWidthList:" + mInfo.toString());
        int count = 30;
        int lineCount = 0;
        if (mInfo.getKuadu()) {// 跨度
            count += 2;
        }
        if (mInfo.getHezhi()) {// 和值
            count += 2;
        }
        if (!mInfo.getKuadu() && !mInfo.getHezhi()) {
            lineCount = 1;
        }
        Log.e("cc", "initWidthList:count:" + count + ";lineCount:" + lineCount + ";viewWidth:"
                + viewWidth);
        widthList = new ArrayList<Float>();
        float item = (viewWidth - lineCount) / (count);

        int i = 0;
        for (i = 0; i < 30; i++) {
            switch (i) {
                case 26:
                    if (mInfo.getKuadu()) {// 跨度
                        widthList.add(item * 2);
                    } else {
                        widthList.add(item * 0);
                    }

                    break;
                case 27:
                    if (mInfo.getHezhi()) {// 和值
                        widthList.add(item * 2);
                    } else {
                        widthList.add(item * 0);
                    }
                    break;
                case 28:
                    widthList.add(item * 2);
                    break;
                case 29:
                    widthList.add(item * 2);
                    break;
                
                default:
                    widthList.add(item);
                    break;
            }
        }
    }
    
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }
    
    @Override
    protected void updateView(List<LotteryInfo> lotteryInfo, String miss, String sum) {
        if (lotteryInfo.isEmpty()) {
            return;
        }
        ArrayList<ArrayList<ArrayList<String>>> data =
                new ArrayList<ArrayList<ArrayList<String>>>();
        int size = lotteryInfo.size();
        
        for (int index = 0; index < size; index++) {
            
            ArrayList<ArrayList<String>> itemList = new ArrayList<ArrayList<String>>();
            LotteryInfo info = lotteryInfo.get(index);
            String value = info.getLotteryNumbers();
            String[] array = value.split(",");
            int arraySize = array.length;
            int[] all = new int[arraySize];
            int count = 0;
            for (int j = 0; j < arraySize; j++) {
                all[j] = Integer.valueOf(array[j]);
                count += all[j];
            }
            int[] aa = {all[0], all[1], all[2]};
            
            String issue = info.getIssue();
            issue = issue.substring(issue.length() - 2, issue.length());
            if (isFirstData(info.getDateline())) {
                
                TableViewUtil.addCol(issue, 4, itemList);// 1
            } else {
                TableViewUtil.addCol(issue, 0, itemList);// 1
            }
            
            TableViewUtil.addCol(array[0], 2, itemList);// 2
            TableViewUtil.addCol(array[1], 2, itemList);// 3
            TableViewUtil.addCol(array[2], 2, itemList);// 4
            TableViewUtil.getDataArray(aa, all, itemList, 11);
            TableViewUtil.getDataArray(aa, itemList, 11);
            TableViewUtil.addCol("" + (all[array.length - 1] - all[0]), 3, itemList);// 25
            TableViewUtil.addCol("" + count, 3, itemList);// 26
            TableViewUtil.addCol(getBigSmall(all, 6), 3, itemList);// 27
            TableViewUtil.addCol(getjiou(all), 3, itemList);// 28
            data.add(itemList);
            
        }
        mTableBadyView.updateData(data, widthList, TableViewUtil.redLine11_5var,
                (textSize + 2) * density, itemHeight);
        
        ArrayList<ArrayList<String>> bottomdata = new ArrayList<ArrayList<String>>();
        ArrayList<String> bottomitem = new ArrayList<String>();
        bottomitem.add("当前遗漏值");
        String[] missArray = miss.split(",");
        // size = missArray.length;
        size = 11;
        for (int index = 0; index < size; index++) {
            bottomitem.add(missArray[index]);
        }
        
        // 01-21 21:38:58.730: V/lss(30044): loadData
        // http://119.10.9.19:8080/le-tou/api/lottery/getLottery?typeCode=hlj11x5&token=B57CAD29EDACE02CDBAF73575D86DC2D&dateline=2016-01-21+13%3A26%3A18
        // 01-21 21:51:24.900: V/lss(30044): loadData
        // http://119.10.9.19:8080/le-tou/api/lottery/getLottery?typeCode=hlj11x5&token=B57CAD29EDACE02CDBAF73575D86DC2D&dateline=2016-01-21+13%3A36%3A18
        
        for (int index = 0; index < size; index++) {
            bottomitem.add(missArray[index + 44]);
        }
        bottomdata.add(bottomitem);
        bottomitem = new ArrayList<String>();
        bottomitem.add("出现总次数");
        String[] sumArray = sum.split(",");
        // size = sumArray.length;
        for (int index = 0; index < size; index++) {
            bottomitem.add(sumArray[index]);
        }
        for (int index = 0; index < size; index++) {
            bottomitem.add(sumArray[index + 44]);
        }
        bottomdata.add(bottomitem);
        ArrayList<Float> list = TableViewUtil.getBottomWidth(widthList, 4, 26, 29);
        mTableBottmBady.addData(bottomdata, TableViewUtil.redL11_5var_bo, list, textSize,
                R.drawable.icon_115);
        scrollview.post(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                scrollview.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
        // bottomText.setSelected(true);
        
        LayoutParams lp = startLotto.getLayoutParams();
        lp.width = list.get(0).intValue() - 1;
        startLotto.setLayoutParams(lp);
        
        lp = menuParent.getLayoutParams();
        float pw = lp.width = list.get(list.size() - 1).intValue();
        menuParent.setLayoutParams(lp);
        
        lp = menuBtn.getLayoutParams();
        Drawable bg = menuBtn.getBackground();
        lp.height = (int) (pw / bg.getIntrinsicWidth() * bg.getIntrinsicHeight());
        menuBtn.setLayoutParams(lp);
    }
    
    @Override
    public void updateTimeCount(String time) {
        if(!TextUtils.isEmpty(time)){
        	 startLotto.setText(time);
        }else{
        	 startLotto.setText(mCountdownTime);	
        }
       
    }
}
